package com.example.up;

import android.support.annotation.NonNull;

import java.io.File;
import java.util.Map;

/**
 * Created by yinghao on 2017/2/10.
 * Email：yhaowa@outlook.com
 */

public class Upload {

    //TODO 保留生产者消费者模式文件池

    private static final String TAG = "Up";

    /**
     * 1. 判断文件是否大于分片大小，小于则直接上传文件，大于则分割
     */

    public static void upLoad(final String url, final File srcFile, final String fileKey, final Map<String, String> params,
                              final OKHttp.ResultCallback<String> callback,
                              @NonNull final OKHttp.ProgressListener progressListener,
                              final OKHttp.RequestInfoListener requestListener) {
        int blockSize = BlockSize.getBlockSize();  //  暂定
        final long fileLength = srcFile.length();
        final long[] readBytesCounts = {0};
        if (srcFile.length() <= blockSize) {
            OKHttp.postAsyn(url, callback, new OKHttp.ProgressListener() {
                @Override
                public void onProgress(long fileLengths, long readBytesCount, boolean blockReadDone,long blockStartTime) {
                    if (readBytesCount == 0) return;
                    readBytesCounts[0] += readBytesCount;
                    progressListener.onProgress(fileLength, readBytesCounts[0], blockReadDone,blockStartTime);
                }
            }, srcFile, fileKey, params, requestListener);
        } else {
            RequestPool blockRequestPool = new RequestPool(url, srcFile, fileKey, params, callback, progressListener, requestListener);
            blockRequestPool.startUpload();
        }
    }


    //保存fileAccess中的位置及文件信息至SP
    public static void pause(String fileName) {
        OKHttp.cancel(fileName);
    }

    //max=64
    public static void setOkMaxRequest(int max) {
        OKHttp.setMaxRequest(max);
    }

    public static void setMaxRequest(int max) {
        RequestPool.setRequestSize(max);
    }

    public static void setOkMaxHostRequest(int max) {
        OKHttp.setMaxHostRequest(max);
    }


}
